//
//  MainTabBarViewController.swift
//  微博
//
//  Created by 段瑞金 on 16/5/11.
//  Copyright © 2016年 段瑞金. All rights reserved.
//

import UIKit

class MainTabBarViewController: UITabBarController {
    //MARK:-懒加载按钮
    private lazy var composeButton : UIButton = {
        
       let composeButton = UIButton(image: "tabbar_compose_icon_add", backGroundImage: "tabbar_compose_button")
        composeButton.sizeToFit()

        return composeButton
    }()

    //将按钮添加到tabbar上面
    //在将按钮添加到tabbar上面的时候,如果在viewdidload中添加,由于之前已经在tabbar上面添加了五个控制器,所以那个空的item会覆盖掉按钮,而如果在viewWillAppear中添加,那么按钮就会在item上面,而不会被覆盖
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        tabBar.addSubview(composeButton)
        composeButton.center.x = tabBar.center.x
        composeButton.addTarget(self, action: #selector(MainTabBarViewController.composeButtonClicked), forControlEvents: .TouchUpInside)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        //添加所有的子控制器
        addChildViewControllers()
    }


}

//MARK:-按钮点击
extension MainTabBarViewController {
    //加号按钮的点击监听
    @objc private func composeButtonClicked () -> (){
        RJLOG("加号按钮")
    }
}


//MARK:-自定义方法

extension MainTabBarViewController {
    //添加所有的子控制器
    private func addChildViewControllers() -> (){
        //获取jsion路径
        let jsonPath = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)!
        //加载jsion文件
        let jsonData = NSData(contentsOfFile: jsonPath)! //由于是本地加载的json文件,所以一定有值,强制解包
        //将jsion文件加载进来的数据转换为对象,字典数组
        //如果加载的有数据,就是用加载额数据,如果没有,就本地创建控制器
        guard let objc = try? NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers) else {
            //首页
            addAllChildsViewController("HomeViewController", title: "首页", normalImage: "tabbar_home")
            //消息
            addAllChildsViewController("MessageViewController", title: "消息", normalImage: "tabbar_message_center")
            //广场
            addAllChildsViewController("DiscoverViewController", title: "广场", normalImage: "tabbar_discover")
            //我
            addAllChildsViewController("ProfileViewController", title: "我", normalImage: "tabbar_profile")
            return
        }
        
        //遍历字典数组,取出对应的值
        guard let controllersArray = objc as? [[String : AnyObject]] else {
            
            return
        }
        
        for dict in controllersArray {
            addAllChildsViewController(dict["vcName"] as? String , title: dict["title"] as? String , normalImage: dict["imageName"] as? String)
        }

    }
    
    //添加子控制器
    private func addAllChildsViewController (childViewControllerName : String? , title : String? , normalImage : String? ) ->(){
        
        //动态获取命名空间
        guard let nameSpace = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as? String else {
            return
        }
        //拼接命名空间生成class
        guard let className : AnyClass = NSClassFromString(nameSpace + "." + (childViewControllerName ?? "")) else{
            return
        }
        
        //告诉编译器获取的类名的真实类型是UIViewController

        guard let vcClass = className as? UIViewController.Type else {
            return
        }
        
        //实例化控制器
        let childViewController = vcClass.init()
        
        childViewController.title = title
        if let imageName = normalImage where imageName != ""{
            
            childViewController.tabBarItem.image = UIImage(named: imageName)
            childViewController.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        }

        addNavigationController(childViewController)
        
        
    }
    //设置导航控制器
    private func addNavigationController (childViewController : UIViewController) ->(){
        let nav = MainNavigationController(rootViewController: childViewController)
        addChildViewController(nav)

    }
}